﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.Statistics
{
    public partial class StatisticsTypeByMonth : BasePage
    {
        DataTable dt = new DataTable();
        public string Tid;
        int year;
        int month;
        protected void Page_Load(object sender, EventArgs e)
        {
            Tid = Request["Y"];
            if (!IsPostBack)
            {
               
                if (DateTime.Now.Month==1)
                {
                    year = DateTime.Now.Year-1;
                    month = 12;
                }
                else
                {
                year = DateTime.Now.Year;
                month = DateTime.Now.Month-1;
                }
                        
                
                dataBind();
                BindChart();
                GetStatistics();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void dataBind()
        {
            SuperviseQuery sq = new SuperviseQuery();
            sq.SortID = Tid;
            //获取当周1号的所在周一日期
            sq.PlanTimeFrom = HZ.Fun2.GetLastMonday(DateTime.Parse(year.ToString() + "-" + (month).ToString() + "-01 00:00:00"));

            try
            {
                sq.PlanTimeTo = DateTime.Parse(year.ToString() + "-" + (month + 1).ToString() + "-01 00:00:00");
                sq.PlanTimeTo = HZ.Fun2.GetNextSaturday(DateTime.Parse(year.ToString() + "-" + (month + 1).ToString() + "-01 00:00:00"));
            }
            catch (Exception)
            {

                sq.PlanTimeTo = DateTime.Parse((year + 1).ToString() + "-01-01 00:00:00");
                sq.PlanTimeTo = HZ.Fun2.GetNextSaturday(DateTime.Parse((year + 1).ToString() + "-01-01 00:00:00"));
            }
            int t = 0;
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            ie.QueryObject = new HZ.Supervise.Model.QueryObject()
            {
                //分组字段
                GroupField = GroupByField.WeekOfYear | GroupByField.SortID | GroupByField.SortSource,
                //查询条件
                Query = sq,
                //分组查询字段
                GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.WareHouseRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseCountTotal,
                //排序字段
                SortField = "WeekOfYear,SortID,SortSource ASC"
            };

            //将数据填入gridview中
            dt = ie.ExecutePage(1, 0, out t);
            //显示相应的字段


            gvwStatisticsType.DataSource = dt;
            gvwStatisticsType.DataBind();
            for (int j = 0; j < gvwStatisticsType.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwStatisticsType.Rows.Count - 1))
                {
                    if (gvwStatisticsType.Rows[j].Cells[0].Text.Trim() == gvwStatisticsType.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwStatisticsType.Rows[j].Cells[0].RowSpan = 2;
                        gvwStatisticsType.Rows[j + 1].Cells[0].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

                //合并
            }
        }
        /// <summary>
        /// 绑定统计
        /// </summary>
        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double PurchaseCountTotal = 0;
            double wareHouseAmountTotal = 0;//入库总金额
            double wareHouseCountTotal = 0;
            for (int i = 0; i < gvwStatisticsType.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwStatisticsType.Rows[i].Cells[2].Text);
                PurchaseCountTotal += double.Parse(gvwStatisticsType.Rows[i].Cells[3].Text);
                wareHouseAmountTotal += double.Parse(gvwStatisticsType.Rows[i].Cells[4].Text);
                wareHouseCountTotal += double.Parse(gvwStatisticsType.Rows[i].Cells[5].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";采购总数量为:" + PurchaseCountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString() + ";入库总数量为:" + wareHouseCountTotal.ToString();

        }

        /// <summary>
        /// 图表绑定 
        /// </summary>
        private void BindChart()
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                string monch = "";//月份
                int count = 2;//次数
                string sort = "0";//属性 0:基药 1:非基药

                #region 左图

                //***************** 基本药品金额*****************/
                Series series1 = new Series("基本药品采购金额");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;
                series1["DrawingStyle"] = "Emboss";
                //***************** 非基本药品金额*******************/
                Series series2 = new Series("非基本药品采购金额");
                series2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series2.BorderWidth = 1;
                series2.ShadowOffset = 1;
                series2["DrawingStyle"] = "Emboss";
                //***********************基本药品数量******************/
                Series series3 = new Series("基本药品采购数量");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                series3["DrawingStyle"] = "Emboss";
                //***********************非基本药品数量******************/
                Series series4 = new Series("非基本药品采购数量");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                series4["DrawingStyle"] = "Emboss";
                #endregion

                #region 右图

                //***************** 基本药品金额 柱状  ****************/
                Series series5 = new Series("基本药品入库金额");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;
                series5["DrawingStyle"] = "Emboss";

                //***************** 非基本药品金额 柱状  **************/
                Series series6 = new Series("非基本药品入库金额");
                series6.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series6.BorderWidth = 1;
                series6.ShadowOffset = 1;
                series6["DrawingStyle"] = "Emboss";
                //***************** 基本药品数量 曲线******************/
                Series series7 = new Series("基本药品入库数量");
                series7.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series7.BorderWidth = 1;
                series7.ShadowOffset = 1;
                series7["DrawingStyle"] = "Emboss";
                //***************** 非基本药品数量 曲线****************/
                Series series8 = new Series("非基本药品入库数量");
                series8.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series8.BorderWidth = 1;
                series8.ShadowOffset = 1;
                series8["DrawingStyle"] = "Emboss";
                #endregion

                #region 绑定值
                int s0 = 0, s1 = 0;
                if (dt.Rows[0]["SortSource"].ToString().Equals("1"))
                {
                    count = 1;
                    monch = dt.Rows[1]["WeekOfYear"].ToString();
                }
                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {
                    if (dt.Rows[i]["WeekOfYear"].ToString().Equals(monch))
                    {
                        if (i == 0)
                        {
                            series1.Points.AddXY(monch + "周", 0);
                            series3.Points.AddXY(monch + "周", 0);
                            series5.Points.AddXY(monch + "周", 0);
                            series7.Points.AddXY(monch + "周", 0);
                            series1.Points[s0].ToolTip = "0";
                            series3.Points[s0].ToolTip = "0";
                            series5.Points[s0].ToolTip = "0";
                            series7.Points[s0].ToolTip = "0";
                            s0++;
                        }
                        series2.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                        series4.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                        series6.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                        series8.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                        series2.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                        series4.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                        series6.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                        series8.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                        s1++;
                        count++;
                        sort = "1";

                    }
                    else
                    {
                        if (count == 2 && dt.Rows[i]["SortSource"].ToString().Equals("0"))
                        {
                            series1.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series3.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                            series5.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                            series7.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                            series1.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                            series3.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                            series5.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                            series7.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                            s0++;
                            count = 1;
                            sort = "0";
                            monch = dt.Rows[i]["WeekOfYear"].ToString();
                        }
                        else
                        {
                            if (sort.Equals("0"))
                            {
                                series2.Points.AddXY(monch + "周", 0);
                                series4.Points.AddXY(monch + "周", 0);
                                series6.Points.AddXY(monch + "周", 0);
                                series8.Points.AddXY(monch + "周", 0);
                                series2.Points[s1].ToolTip = "0";
                                series4.Points[s1].ToolTip = "0";
                                series6.Points[s1].ToolTip = "0";
                                series8.Points[s1].ToolTip = "0";
                                s1++;
                            }
                            else
                            {
                                series1.Points.AddXY(monch + "周", 0);
                                series3.Points.AddXY(monch + "周", 0);
                                series5.Points.AddXY(monch + "周", 0);
                                series7.Points.AddXY(monch + "周", 0);
                                series1.Points[s0].ToolTip = "0";
                                series3.Points[s0].ToolTip = "0";
                                series5.Points[s0].ToolTip = "0";
                                series7.Points[s0].ToolTip = "0";
                                s0++;
                            }

                            if (dt.Rows[i]["SortSource"].ToString().Equals("0"))
                            {
                                series1.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series3.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                                series5.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                                series7.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                                series1.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                                series3.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                                series5.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                                series7.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                                s0++;
                                monch = dt.Rows[i]["WeekOfYear"].ToString();
                                count = 1;
                                sort = "0";
                            }
                            else
                            {
                                series2.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series4.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                                series6.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                                series8.Points.AddXY(dt.Rows[i]["WeekOfYear"].ToString() + "周", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                                series2.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                                series4.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                                series6.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                                series8.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                                s1++;
                                count = 2;
                                sort = "1";

                            }

                        }

                    }
                }
                #endregion

                #region 数据绑定到图

                series1.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series1);
                ChartPurchasing.Series["基本药品采购金额"].ChartArea = "ChartTypePurchasing";

                series2.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series2);
                ChartPurchasing.Series["非基本药品采购金额"].ChartArea = "ChartTypePurchasing";

                series3.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series3);
                ChartInStorage.Series["基本药品采购数量"].ChartArea = "ChartTypeStorage";

                series4.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series4);
                ChartInStorage.Series["非基本药品采购数量"].ChartArea = "ChartTypeStorage";

                series5.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series5);
                ChartPurchasing.Series["基本药品入库金额"].ChartArea = "ChartTypePurchasing";


                series6.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series6);
                ChartPurchasing.Series["非基本药品入库金额"].ChartArea = "ChartTypePurchasing";

                series7.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series7);
                ChartInStorage.Series["基本药品入库数量"].ChartArea = "ChartTypeStorage";

                series8.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series8);
                ChartInStorage.Series["非基本药品入库数量"].ChartArea = "ChartTypeStorage";
                #endregion

                #region 两张图片属性

                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].Area3DStyle.Enable3D = false;

                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisX.IsMarginVisible = true;
                ChartInStorage.ChartAreas["ChartTypeStorage"].Area3DStyle.Enable3D = false;


                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.MajorGrid.Interval = 1;
                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisX.MajorGrid.Interval = 1;
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisY.LineWidth = 1;
                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisY.LineWidth = 1;

                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisY.Title = "金额（万元）";
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.Title = "周";

                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisY.Title = "数量";
                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisX.Title = "周";

                #endregion

                #region 分组
                ChartPurchasing.Series["基本药品采购金额"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["非基本药品采购金额"]["StackedGroupName"] = "Group1";
                ChartInStorage.Series["基本药品采购数量"]["StackedGroupName"] = "Group1";
                ChartInStorage.Series["非基本药品采购数量"]["StackedGroupName"] = "Group1";

                ChartPurchasing.Series["基本药品入库金额"]["StackedGroupName"] = "Group2";
                ChartPurchasing.Series["非基本药品入库金额"]["StackedGroupName"] = "Group2";
                ChartInStorage.Series["基本药品入库数量"]["StackedGroupName"] = "Group2";
                ChartInStorage.Series["非基本药品入库数量"]["StackedGroupName"] = "Group2";
                ChartInStorage.ResetAutoValues();
                ChartPurchasing.ResetAutoValues();
                #endregion
                lblnull.Visible = false;

            }
            else
            {
                this.ChartInStorage.Visible = false;
                this.ChartPurchasing.Visible = false;
                lblnull.Visible = true;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!this.droYear.SelectedValue.Equals("-1") && !this.droMonth.SelectedValue.Equals("-1"))
            {
                this.month = int.Parse(this.droMonth.SelectedValue);
                this.year = int.Parse(this.droYear.SelectedValue);
                dataBind();
                BindChart();
                GetStatistics();
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择周')</script>");
            }
        }
    }
}
